//index.js
//获取应用实例
var app = getApp();

Page({
    data: {
        scrollTop: {
            scroll_top: 0,
        },
        isFixed:false,
        isshow_subsidy: true,
        minprice:'',
        maxprice:'',
        redTriangle: false,
        isShow0: false,
        isShow1: false,
        isShow2: false,
        sort: 0,
        cname: "",
        sortName: "默认",
        currentTab: 0,
        currentTab2: 0,
        hiddenLoading: false,
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        from_uid: 0,
        nickname:'',
        memberid: 0,
        datalock: false,
        uri: "list",
        searchkey: "",
        showAnimation:false,
        globalset: {

        },
        homeskinid:-1,
        duration: 100,
        "banner": [
        ],
        commodity: [

        ],
        goodCarousel: [

        ],
        goodCarousel2: [

        ],
        
        activitymenu: [
            
        ],
    },

    onLoad: function (options) {

        //app.setTabBar();
        var $this=this;
        //扫二维码进来的推荐人ID获取
        if (typeof options !="undefined" && typeof options.scene != "undefined") {
            wx.setStorage({
                key: "from_uid",
                data: options.scene
            });
            this.setData({
                from_uid: options.scene,
                datalock: false
            });
            
        }
        
        
        
        if (typeof options != "undefined" && typeof options.from_uid != "undefined") {
            wx.setStorage({
                key: "from_uid",
                data: options.from_uid
            });
            this.setData({
                from_uid: options.from_uid,
                datalock: false
            });
        } 
        if (typeof options != "undefined"  && typeof options.from_nickname != 'undefined' && options.type==1){
          /*
          wx.setNavigationBarTitle({
            title: options.from_nickname + '的小店'//页面标题
          });
          */
          /*
          this.setData({
            homeskinid: options.homeskinid,
            datalock: false
          });
          */
        }
        
        this.getGlobalSet();
        //控制弹窗的函数
     
        
        
        //获取缓存中的searchkey
        //判断剪贴班中是否有内容

        //判断是否先显示补助
        
      

    },
    initIndex:function(){
      var $this = this;
     // $this.sign();

      $this.goodList(1,6);
      $this.goodList(1,7);
      $this.goodList(1,8);

      
      var memberinfo = wx.getStorageSync('memberinfo');
      var globalset = wx.getStorageSync('globalset');
      $this.setData({
        memberinfo:memberinfo,
        commission: 0,
        datalock: false
      });
      //计算总的佣金比例
      var total_commission = 0;
      if (globalset.memberlevel != undefined && globalset.memberlevel.length != undefined) {
        for (var i = 0; i < globalset.memberlevel.length; i++) {
         
          //当前会员类型的等级佣金比例
          if (globalset.memberlevel[i].type == memberinfo.type && globalset.memberlevel[i].name == memberinfo.level) {
            total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2) + parseFloat(globalset.memberlevel[i].myleader1_credit2) + parseFloat(globalset.memberlevel[i].myleader2_credit2);
            //换成只取1级佣金的比例
            total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2);
         
            $this.setData({
              commission: total_commission,
              datalock: false
            });
          
          }
        }
      }
      if (memberinfo.type == 0) {
        if (globalset.isopen_subsidy == 0) {
          $this.setData({
            isshow_subsidy: false
          });
        }
      
        total_commission = parseFloat(globalset.subsidy);
        $this.setData({
          commission: total_commission,
          datalock: false
        });
      }

      if (globalset.isshow_subsidy == 0) {
        $this.setData({
          isshow_subsidy: false
        });
      }
    },
    getGlobalSet: function () {
        var $this = this;
        app.util.request({
            url: 'entry/wxapp/Global',
            showLoading: false,
            data: {
                "m": 'nets_haojk'
            },
            method: 'post',
            success: function (response) {
                var globalset = response.data.data;
                $this.setData({
                  hiddenLoading: true
                });
                $this.setData({
                    globalset: globalset,
                    homeskinid: globalset.homeskinid
                });
                wx.setStorage({
                    key: "globalset",
                    data: globalset
                });
                //banner
                var banner_str = globalset.banner;
                var banner_json = JSON.parse(banner_str);
                if (banner_json != undefined && banner_json.list != null && banner_json.list != undefined && typeof banner_json.list != undefined && banner_json.list != null && banner_json.list != '') {
                $this.setData({
                    banner: banner_json.list,
                    datalock: false
                });
                }
                //图标菜单
                var newhead_menu_json = [];
                var head_menu_str = globalset.head_menu;
                var head_menu_json = JSON.parse(head_menu_str);
          
              
                if (head_menu_json != undefined && head_menu_json.list != null && head_menu_json.list!=undefined && typeof head_menu_json.list != undefined && head_menu_json.list != null && head_menu_json.list!=''){
                var menulen = head_menu_json.list.length;
                for (var i = 0; i < 8; i++) {
                    newhead_menu_json.push(head_menu_json.list[i])
                }


                $this.setData({
                    imgmenu: newhead_menu_json,
                    datalock: false

                });
              

                var imgmenu2 = new Array();
                for (var i = 8; i < 16; i++) {
                    imgmenu2.push(head_menu_json.list[i]);
                }
                $this.setData({
                    imgmenu2: imgmenu2,
                    datalock: false
                });
               
                $this.setData({
                    imgmenuAll: head_menu_json.list,
                    datalock: false
                });
              
              
                var imgmenu22 = new Array();
                for (var i = 16; i < menulen; i++) {
                    imgmenu22.push(head_menu_json.list[i]);
                }
                $this.setData({
                    imgmenu22: imgmenu22,
                    datalock: false
                });
                var imgmenu222 = new Array();
                for (var i = 24; i < menulen; i++) {
                    imgmenu222.push(head_menu_json.list[i]);
                }
                $this.setData({
                    imgmenu222: imgmenu222,
                    datalock: false
                });
                }
                //图片魔方菜单
                var picture_menu_str = globalset.picture_menu;
                var picture_menu_json = JSON.parse(picture_menu_str);
                var picture_menu = new Array();
                var pmenu = new Object();
              
                if (picture_menu_json != undefined && typeof picture_menu_json.list != undefined && picture_menu_json.list != null  && picture_menu_json.list!=null && picture_menu_json.list.length>=4){
                  pmenu.img1 = typeof picture_menu_json.list[0].img == "undefined" ? "" : picture_menu_json.list[0].img;
                  pmenu.name1 = typeof picture_menu_json.list[0].name == "undefined" ? "" : picture_menu_json.list[0].name;
                  pmenu.url1 = typeof picture_menu_json.list[0].url == "undefined" ? "" : picture_menu_json.list[0].url;

                  pmenu.img2 = typeof picture_menu_json.list[1].img == "undefined" ? "" : picture_menu_json.list[1].img;
                  pmenu.name2 = typeof picture_menu_json.list[1].name == "undefined" ? "" : picture_menu_json.list[1].name;
                  pmenu.url2 = typeof picture_menu_json.list[1].url == "undefined" ? "" : picture_menu_json.list[1].url;
                  pmenu.img3 = typeof picture_menu_json.list[2].img == "undefined" ? "" : picture_menu_json.list[2].img;
                  pmenu.name3 = typeof picture_menu_json.list[2].name == "undefined" ? "" : picture_menu_json.list[2].name;
                  pmenu.url3 = typeof picture_menu_json.list[2].url == "undefined" ? "" : picture_menu_json.list[2].url;
                  pmenu.img4 = typeof picture_menu_json.list[3].img == "undefined" ? "" : picture_menu_json.list[3].img;
                  pmenu.name4 = typeof picture_menu_json.list[3].name == "undefined" ? "" : picture_menu_json.list[3].name;
                  pmenu.url4 = typeof picture_menu_json.list[3].url == "undefined" ? "" : picture_menu_json.list[3].url;
                }else{
                  pmenu.img1 = "";
                  pmenu.name1 = "";
                  pmenu.url1 = "";
                  pmenu.img2 = "";
                  pmenu.name2 = "";
                  pmenu.url2 = "";
                  pmenu.img3 = "";
                  pmenu.name3 = "";
                  pmenu.url3 = "";
                  pmenu.img4 = "";
                  pmenu.name4 = "";
                  pmenu.url4 = "";
                }
                picture_menu.push(pmenu);
                $this.setData({
                    imgmenu3: picture_menu,
                    datalock: false
                });

                //活动图片
                var picture_menu2_str = globalset.picture_menu2;
                var picture_menu2_json = JSON.parse(picture_menu2_str);
                if (picture_menu2_json != undefined && picture_menu2_json.list != null && picture_menu2_json.list != undefined && typeof picture_menu2_json.list != undefined && picture_menu2_json.list != null && picture_menu2_json.list != '') {
                $this.setData({
                    activitymenu: picture_menu2_json.list,
                    datalock: false
                });
			
                }
                //广告图片
                var ad_menu_str = globalset.ad_menu;
                var ad_menu_json = JSON.parse(ad_menu_str);
                if (ad_menu_json != undefined && ad_menu_json.list != null && ad_menu_json.list != undefined && typeof ad_menu_json.list != undefined && ad_menu_json.list != null && ad_menu_json.list != '') {
                $this.setData({
                    admenu: ad_menu_json.list,
                    datalock: false
                });
				
                }
                $this.setData({
                    hiddenLoading: true
                });
                $this.getMember();
                
            }
        });

    },
    getMember: function () {
        var that = this;
        app.getUserInfo(function (userInfo) {
            //更新数据
            app.util.getUserInfo(function (response) {

                response.memberInfo.nickname = userInfo.nickName;
                response.memberInfo.avatar = userInfo.avatarUrl;
                response.memberInfo.city = userInfo.city;
                response.memberInfo.country = userInfo.country;
                response.memberInfo.province = userInfo.province;
                response.memberInfo.gender = userInfo.gender;
                that.addMember(response.memberInfo);
            });
        });
        // 页面渲染完成
    },
 
    addMember: function (userInfo) {
        var $this = this;
        app.util.request({
            url: 'entry/wxapp/AddMember',
            showLoading: false,
            data: {
                "m": 'nets_haojk',
                "from_uid": this.data.from_uid,
                "nickname": userInfo.nickname,
                "avatar": userInfo.avatar,
                "city": userInfo.city,
                "country": userInfo.country,
                "province": userInfo.province,
                "gender": userInfo.gender,
            },
            method: 'post',
            success: function (response) {
                //首页存储会员对象到缓存，其他页面直接读取缓存即可
                wx.setStorage({
                    key: "memberinfo",
                    data: response.data.data
                });
                $this.setData({
                    memberid: response.data.data.memberid,
                    nickname:response.data.data.nickname,
                    datalock: false
                });
                //取会员对象的方式
                wx.getStorage({
                    key: 'memberinfo',
                    success: function (res) {
                    }
                });
                // if (typeof response.data.data.from_nickname != 'undefined' && response.data.data.from_nickname != '' && response.data.data.from_nickname != '无') {
                //   wx.setNavigationBarTitle({
                //     title: response.data.data.from_nickname + '的小店'//页面标题
                //   });
                // }
                // if (typeof response.data.data.nickname != 'undefined' && response.data.data.nickname != '' && response.data.data.type == 1) {
                //   wx.setNavigationBarTitle({
                //     title: response.data.data.nickname + '的小店'//页面标题

                //   });
                //   $this.setData({
                //     homeskinid: response.data.data.homeskinid,
                //     datalock: false
                //   });
                // }
                // if (typeof response.data.data.shopname != 'undefined' && response.data.data.shopname != '' && response.data.data.type == 1) {
                //   wx.setNavigationBarTitle({
                //     title: response.data.data.shopname//页面标题

                //   });
                //   $this.setData({
                //     homeskinid: response.data.data.homeskinid,
                //     datalock: false
                //   });
                // }
                $this.initIndex();
                
            }
        });
    },
   
    // sign: function () {
    //     app.util.request({
    //         url: 'entry/wxapp/Sign',
    //         showLoading: false,
    //         data: {
    //             "m": 'nets_haojk'
    //         },
    //         method: 'post',
    //         success: function (response) {
    //             if (response.data.message == "") {
    //                 return;
    //             }
    //             //签到成功
    //             wx.showModal({
    //                 title: '签到成功',
    //                 content: response.data.message,
    //                 success: function (res) {

    //                 }
    //             });
    //         }
    //     });
    // },

    searchkey: function (e) {
        wx.setStorageSync("searchkey", this.data.searchkey);
        wx.setStorageSync('limittime', '');
        wx.setStorageSync('realtime', '');

        if (this.data.searchkey != "") {
            wx.navigateTo({
                url: "../search/index",

            })
        } else {

        }

    },
   
    bindKeyInput: function (e) {
        var value = e.detail.value;
        var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
        var objExp = new RegExp(Expression);
        if (objExp.test(value) == true) {
            //是URL
            if(value.indexOf(".html")>0){
              value = value.split('?')[0];
              var valuearr = value.split("/");
              value = valuearr[valuearr.length - 1];
              value = value.replace(".html", "");
            }
            if (value.indexOf("sku=") > 0) {
              value = value.split('sku=')[1];
              var valuearr = value.split("&");
              value = valuearr[0];
            }
        }
        this.setData({
            searchkey: value
        });
        wx.setStorage({
            key: "searchkey",
            data: value
        });
        this.setData({
            keyword: value,
            cname: "",
            uri: "listbysearch"
        });
    },
    onShareAppMessage: function (res) {
      var memberinfo=wx.getStorageSync("memberinfo");
        if (res.from === 'button') {
            // 来自页面内转发按钮
        }

        return {
            title: this.data.globalset.title,
            path: '/haojk/pages/index/index?from_uid=' + memberinfo.memberid + '&from_nickname=' + memberinfo.nickname + '&type=' + memberinfo.type + '&homeskinid=' + memberinfo.homeskinid,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }

    },

    goodList: function (page, t, callback,goodstype) {
        var $this = this;

        $this.setData({
            page: page,
            datalock: true,
        });

        app.util.request({
            url: 'entry/wxapp/Goods',
            showLoading: false,
            data: {
                m: 'nets_haojk',
                page: page,
                uri: $this.data.uri,

                sort:t,
                cname: $this.data.cname,
              
                goodstype: 0
            },
            method: 'post',
            success: function (response) {
                if (typeof callback == 'function') {
                    callback(response.data.data);
                    $this.setData({
                        datalock: false
                    });
                } else {
                     var goods = $this.data.commodity;
                     if(page==1){
                       goods=new Array();
                     }
                    var shishigoods=Array();
                    var jinrigoods=Array();

                    if (response.data.data) {
                        var row=0;
                        for (var i in response.data.data) {
                            row++;
                            if ($this.data.commission == 0) {
                                response.data.data[i].wlPrice_after1 = response.data.data[i].discount;
                            } else {
                                response.data.data[i].wlPrice_after1 = response.data.data[i].wlPrice_after * (response.data.data[i].wlCommissionShare / 100) * ($this.data.commission / 100)*0.9;
                                if (response.data.data[i].wlPrice_after1 > 0)
                                    response.data.data[i].wlPrice_after1 = response.data.data[i].wlPrice_after1.toFixed(2);
                             
                            }
                            if (t == 6 && row <= 6){
                              
                                shishigoods.push(response.data.data[i]);
                               
                            } else if (t == 7 && row <= 6){
                              
                                jinrigoods.push(response.data.data[i]);
                               
                            }else{
                              goods.push(response.data.data[i]);
                            }
                        }
                    }
                    if (t == 6 && page == 1) {
                        $this.setData({
                            goodCarousel: shishigoods,
                            datalock: false
                        });
                      
                    } else if (t == 7 && page == 1 ) {
                        $this.setData({
                            goodCarousel2: jinrigoods,
                            datalock: false
                        });
                       
                    } else if (t == 8){
                        $this.setData({
                            commodity: goods,
                            datalock: false
                        });
                    }
                }


            }
        });
    },
  
    //事件处理函数
    skiptodetail: function (e) {

        var skuId = e.currentTarget.dataset.skuid;
        for (var i = 0; i < this.data.commodity.length; i++) {
            if (this.data.commodity[i].skuId == skuId) {
                var good = this.data.commodity[i];
                wx.setStorage({
                    key: "good",
                    data: good
                });
            }
        }

        for (var i = 0; i < this.data.goodCarousel.length; i++) {
            if (this.data.goodCarousel[i].skuId == skuId) {
                var good = this.data.goodCarousel[i];
                wx.setStorage({
                    key: "good",
                    data: good
                });
            }
        }
        for (var i = 0; i < this.data.goodCarousel2.length; i++) {
            if (this.data.goodCarousel2[i].skuId == skuId) {
                var good = this.data.goodCarousel2[i];
                wx.setStorage({
                    key: "good",
                    data: good
                });
            }
        }





        wx.navigateTo({
            url: '../detail/index?skuId=' + skuId
        })
    },
    onReachBottom: function () {
        
        var $this = this;
    
        var page = $this.data.page + 1;
        var second_height = $this.data.second_height * page;
        $this.setData({ second_height: second_height });
        $this.goodList(page,8);

    },
    limittimeMore: function (e) {
        wx.navigateTo({
            url: "../seniorityTwohours/index",
        })
    },
    realtimeMore: function (e) {
        wx.navigateTo({
            url: "../seniorityAlldays/index",
        })
    },
   

})
